import wx
import time
class SearchPanel ( wx.Panel ):
    title = 'Search Replace'
    instance = None

    def __init__( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 800, -1 ), style = wx.TAB_TRAVERSAL, name = wx.EmptyString ):
        wx.Panel.__init__ ( self, parent, id = id, pos = pos, size = size, style = style, name = name )

        sizer = wx.BoxSizer( wx.HORIZONTAL )

        self.btn_re = wx.BitmapButton( self, wx.ID_ANY, wx.Bitmap('./icons/正则.png'), wx.DefaultPosition, wx.DefaultSize, wx.BU_AUTODRAW|0 )
        sizer.Add( self.btn_re, 0, wx.ALL, 5 )

        self.btn_case = wx.BitmapButton( self, wx.ID_ANY, wx.Bitmap('./icons/大小写.png'), wx.DefaultPosition, wx.DefaultSize, wx.BU_AUTODRAW|0 )
        sizer.Add( self.btn_case, 0, wx.ALL, 5 )

        self.btn_wrap = wx.BitmapButton( self, wx.ID_ANY,wx.Bitmap('./icons/循环搜索.png'), wx.DefaultPosition, wx.DefaultSize, wx.BU_AUTODRAW|0 )
        sizer.Add( self.btn_wrap, 0, wx.ALL, 5 )

        self.txt_search = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        sizer.Add( self.txt_search, 1, wx.ALL, 5 )

        self.btn_pre = wx.Button( self, wx.ID_ANY, u"上一个", wx.DefaultPosition, wx.DefaultSize, 0 )
        sizer.Add( self.btn_pre, 0, wx.ALL, 5 )

        self.btn_next = wx.Button( self, wx.ID_ANY, u"下一个", wx.DefaultPosition, wx.DefaultSize, 0 )
        sizer.Add( self.btn_next, 0, wx.ALL, 5 )

        self.txt_replace = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        sizer.Add( self.txt_replace, 0, wx.ALL, 5 )

        self.btn_replace = wx.Button( self, wx.ID_ANY, u"替换", wx.DefaultPosition, wx.DefaultSize, 0 )
        sizer.Add( self.btn_replace, 0, wx.ALL, 5 )

        self.btn_replace_all = wx.Button( self, wx.ID_ANY, u"全部替换", wx.DefaultPosition, wx.DefaultSize, 0 )
        sizer.Add( self.btn_replace_all, 0, wx.ALL, 5 )

        self.m_staticline1 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        sizer.Add( self.m_staticline1, 0, wx.ALL, 5 )

        
        self.btn_close = wx.Button( self, wx.ID_ANY, u"X", wx.DefaultPosition, wx.DefaultSize, wx.BU_EXACTFIT )
        self.btn_close.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_ACTIVECAPTION ) )

        sizer.Add( self.btn_close, 0, wx.ALL|wx.EXPAND, 0 )

        self.SetSizer( sizer )
        self.Layout()
        self.Fit()

        self.cursor = 0
        self.btn_close.Bind(wx.EVT_BUTTON, self.on_close)
        
        self.btn_next.Bind(wx.EVT_BUTTON, self.on_next)
        self.btn_pre.Bind(wx.EVT_BUTTON, self.on_prev)
        
        self.btn_replace.Bind(wx.EVT_BUTTON, self.on_replace)
        self.btn_replace_all.Bind(wx.EVT_BUTTON, self.on_replace_all)

        #self.searchpad = SearchPanel(self)
        parent.m_mgr.AddPane( self, wx.aui.AuiPaneInfo().Top().CaptionVisible( False ).PinButton( True ).Dock().Resizable().FloatingSize( wx.DefaultSize ).Layer( 5 ) )

    @classmethod
    def show(cls, parent, word=None, visible=None):
        if cls.instance is None:
            cls.instance = cls(parent)
            parent.m_mgr.Update()
        else:
            info = parent.m_mgr.GetPane(cls.instance)
            info.Show(not info.IsShown() if visible is None else visible)
            parent.m_mgr.Update()
        if not word is None: cls.instance.txt_search.SetValue(word)

    def on_next(self, event):
        val = self.txt_search.GetValue()
        print('search ', val)
        parent = self.GetParent()
        current_page = parent.page.GetCurrentPage()
        current_page.search_next(val)
        current_page.SetFocus()
        
    def on_prev(self, event):
        val = self.txt_search.GetValue()
        print('search ', val)
        parent = self.GetParent()
        current_page = parent.page.GetCurrentPage()
        current_page.search_prev(val)
        current_page.SetFocus()
    
    def on_replace(self, event):
        source = self.txt_search.GetValue()
        target = self.txt_replace.GetValue()
        
        parent = self.GetParent()
        current_page = parent.page.GetCurrentPage()
        
        selection = current_page.GetSelectedText()
        print('selection:', selection)
        if selection == source:
            current_page.ReplaceSelection(target)
            current_page.SetFocus()

    def on_replace_all(self, event):
        source = self.txt_search.GetValue()
        target = self.txt_replace.GetValue()
        
        parent = self.GetParent()
        current_page = parent.page.GetCurrentPage()
        
        selection = current_page.GetSelectedText() 
        print('selection:', selection)
           
        # 如果选中的就是source，则清楚selection替换全局
        if selection == source or selection == '':
            current_page.SelectAll()
            content_sel = current_page.GetSelectedText()
            content_sel_rep = content_sel.replace(source, target)
            current_page.ReplaceSelection(content_sel_rep)
        else:
            content_sel = current_page.GetSelectedText()
            content_sel_rep = content_sel.replace(source, target)
            current_page.ReplaceSelection(content_sel_rep)
            
        
    def on_close(self, event):
        SearchPanel.show(self.GetParent(), visible=False)

    def __del__( self ):
        pass

if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame(None)
    SearchPanel(frame)
    frame.Show()
    frame.Fit()
    app.MainLoop()